import logging

import pytest

from resource.pom.music import Music
from utils.manager import Images


class Test:
    app = Music

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：点击音乐应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.stop_permission()
        logging.info('预期结果1.正常进入音乐应用内')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.播放的音乐点击back键后再进入')
        # 先点击播放
        device.refresh_layout()
        play = device.get_element_by_key('image3')
        x1, y1, x2, y2 = device.get_element_area(play)
        crop_area = (x1 + 50, y1 + 50, x2 - 50, y2 - 50)
        device.click_element(play)
        device.wait(3)
        device.go_back()
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.wait(2)
        logging.info('预期结果2.点击back后再进入音乐会暂停')
        pause0_pic = device.save_snapshot_to_local('pause0.jpeg')
        Images.crop_picture(pause0_pic, *crop_area)
        similarity0 = Images.compare_image_similarity(pause0_pic, self.app.standard_pause_pic)
        assert similarity0 > 0.6, '暂停状态的截图对比和标准图相似度过低'

        logging.info('步骤3.播放的音乐点击home键后再进入')
        device.swipe(500, 979, 100, 979)
        device.click_element(play)
        device.wait(3)
        device.go_home()
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.wait(2)
        logging.info('预期结果3.点击home键后再进入音乐不会暂停')
        running0_pic = device.save_snapshot_to_local('running0.jpeg')
        Images.crop_picture(running0_pic, *crop_area)
        similarity1 = Images.compare_image_similarity(running0_pic, self.app.standard_running_pic)
        assert similarity1 > 0.6, '播放状态的截图对比和标准图相似度过低'

        logging.info('步骤4.播放的音乐点击recent键再进入')
        device.press_recent_key()
        device.wait(0.5)
        device.click(360, 640)
        device.wait(2)
        logging.info('预期结果4.点击recent键后再进入音乐不会暂停')
        running1_pic = device.save_snapshot_to_local('running1.jpeg')
        Images.crop_picture(running1_pic, *crop_area)
        similarity2 = Images.compare_image_similarity(running1_pic, self.app.standard_running_pic)
        assert similarity2 > 0.6, '播放状态的截图对比和标准图相似度过低'